/**
 * @Title: PayTypeLayout.java
 * @Description: TODO
 * @author Mengting Lu
 * @date 2015年8月24日 下午3:12:33
 */
package com.shanghaionstar.enumtype;

import android.app.Dialog;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.smart.entity.PayChannelResponseEntity;
import com.shanghaionstar.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Mengting Lu
 * @ClassName: PayTypeLayout
 * @Description:点击微信支付会跳出几个支付方式
 * @date 2015年8月24日 下午3:12:33
 */
public class PayTypeLayout extends LinearLayout implements OnClickListener {
    @FindViewById
    private View bt_car_select_confirm;
    @FindViewById
    private RelativeLayout alipay_app_layout, alipay_web_layout, wechat_layout;
    @FindViewById
    private ImageView iv_alipay_app, iv_alipay_web, iv_wechat;
    private View view;
    private Dialog popDialog;
    private OnSelectPayTypeItemOnClickListener selectListener;
    private Map<PayTypeEnum, String> payTypeStrs = new HashMap<>();
    List<PayChannelResponseEntity.ChannelsBean> paychannels;
    //
    private TextView pay_tv_01, pay_tv_02, pay_tv_03;

    public PayTypeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    public PayTypeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public PayTypeLayout(Context context, List<PayChannelResponseEntity.ChannelsBean> paychannels) {
        super(context);
        this.paychannels = paychannels;
        initView();
    }

    private void initView() {
        view = inflate(getContext(), R.layout.onstar_payme_paytype_select_dialog, this);
        alipay_app_layout = (RelativeLayout) view.findViewById(R.id.alipay_app_layout);
        alipay_web_layout = (RelativeLayout) view.findViewById(R.id.alipay_web_layout);
        wechat_layout = (RelativeLayout) view.findViewById(R.id.wechat_layout);
        iv_alipay_app = (ImageView) view.findViewById(R.id.alipay_app_select);
        iv_alipay_web = (ImageView) view.findViewById(R.id.alipay_web_select);
        iv_wechat = (ImageView) view.findViewById(R.id.wechat_select);

        //
        pay_tv_01 = (TextView) view.findViewById(R.id.pay_tv_01);
        pay_tv_02 = (TextView) view.findViewById(R.id.pay_tv_02);
        pay_tv_03 = (TextView) view.findViewById(R.id.pay_tv_03);


        bt_car_select_confirm = view.findViewById(R.id.bt_car_select_confirm);

        bt_car_select_confirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
            }
        });

        iv_wechat.setImageResource(R.drawable.change_vehicle_check);

        alipay_app_layout.setOnClickListener(this);
        alipay_web_layout.setOnClickListener(this);
        wechat_layout.setOnClickListener(this);


        //默认标记全都不选中
        iv_alipay_app.setImageResource(NO_ID);
        iv_alipay_web.setImageResource(NO_ID);
        iv_wechat.setImageResource(NO_ID);

        for (int i = 0; i < paychannels.size(); i++) {
            String channel = paychannels.get(i).getChannel();
            String payName = paychannels.get(i).getDescription();
            if (channel.equals("WeChat")) {
                payTypeStrs.put(PayTypeEnum.WECHATAPP, payName);
                pay_tv_01.setText(payName);
            }
            if (channel.equals("alipay")) {
                payTypeStrs.put(PayTypeEnum.ALIPAYAPP, payName);
                pay_tv_02.setText(payName);
            }
            if (channel.equals("webpay")) {
                payTypeStrs.put(PayTypeEnum.ALIPAYWEB, payName);
                pay_tv_03.setText(payName);
            }


            //显示默认
            String isDdefault = paychannels.get(i).getIsDefault();
            if (isDdefault.equals("1")) {
                if (channel.equals("WeChat")) {
                    iv_wechat.setImageResource(R.drawable.change_vehicle_check);
                } else if (channel.equals("alipay")) {
                    iv_alipay_app.setImageResource(R.drawable.change_vehicle_check);
                } else if (channel.equals("webpay")) {
                    iv_alipay_web.setImageResource(R.drawable.change_vehicle_check);
                }
            }
        }

    }

    public String getPayType(PayTypeEnum payTypeEnum) {
        return payTypeStrs.get(payTypeEnum);
    }

    @Override
    public void onClick(View v) {
        iv_alipay_app.setImageResource(NO_ID);
        iv_alipay_web.setImageResource(NO_ID);
        iv_wechat.setImageResource(NO_ID);
        switch (v.getId()) {
            case R.id.alipay_app_layout:
                iv_alipay_app.setImageResource(R.drawable.change_vehicle_check);
                selectListener.onSelectItem(PayTypeEnum.ALIPAYAPP);
                close();
                break;
            case R.id.alipay_web_layout:
                iv_alipay_web.setImageResource(R.drawable.change_vehicle_check);
                selectListener.onSelectItem(PayTypeEnum.ALIPAYWEB);
                close();
                break;
            case R.id.wechat_layout:
                iv_wechat.setImageResource(R.drawable.change_vehicle_check);
                selectListener.onSelectItem(PayTypeEnum.WECHATAPP);
                close();
                break;
            default:
                break;
        }
    }

    /**
     * Colse pay type dialog
     */
    public void close() {
        if (popDialog.isShowing()) {
            popDialog.dismiss();
        }
    }

    /**
     * Show pay type dialog
     *
     * @param
     */
    public void show() {
        if (popDialog == null) {
            popDialog = new Dialog(getContext(), R.style.dialog);
            Window window = popDialog.getWindow();
            window.setWindowAnimations(R.style.share_layout_animation);
            window.setBackgroundDrawableResource(R.color.touming);
            WindowManager.LayoutParams lp = window.getAttributes();
            DisplayMetrics d = getContext().getResources().getDisplayMetrics(); // 获取屏幕宽、高用
            lp.width = (int) (d.widthPixels * 0.9); // 高度设置为屏幕的0.6
            lp.height = (int) (d.heightPixels * 0.5);
            window.setAttributes(lp);
            window.setGravity(Gravity.CENTER);

            popDialog.addContentView(view, lp);
        }
        if (!popDialog.isShowing()) {
            popDialog.show();
        }
    }

    public void setPayTypeItemSelectListener(OnSelectPayTypeItemOnClickListener selectListener) {
        this.selectListener = selectListener;
    }

    public interface OnSelectPayTypeItemOnClickListener {
        void onSelectItem(PayTypeEnum payTypeEnum);
    }

}
